package com.bbcare.treat.plat.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ResultModel;
import com.bbcare.comm.SeqUtil;
import com.bbcare.comm.ToolUtils;
import com.bbcare.treat.plat.service.IPublicConfigService;

/**
 * @name: 随访诊疗系统（基因等）项目管理
 * @author: DemonLee
 * @createTime: 2017.11.29
 * @description: 随访诊疗系统（基因等）项目管理相关操作
 * @modify:
 *
 */
@Controller
@RequestMapping("/service/publicApi")
public class PublicConfigController {
    private final Log logger = LogFactory.getLog(PublicConfigController.class);

    @Autowired
    private IPublicConfigService publicConfigService;

    // 查询项目
    @ResponseBody
    @RequestMapping("/queryProjectItem")
    public ResultModel<Map<String, Object>> queryProjectItem(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in queryProjectItem----arg==" + arg);

        // 基础校验
        ToolUtils.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        Map<String, Object> mapIn = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

        // 入参校验
        Map<String, Object> chkVar = new HashMap<String, Object>();
        chkVar.put("configCode", "题型id");
        chkVar.put("configType", "类型编码");
        ToolUtils.checkInputInfo(Constants.INF_INPUTPARAM_COMMONFIELD, mapIn, chkVar);

        // 登录信息，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        mapIn.put("tenantId", sessionMap.get("tenantId"));

        Map<String, Object> retMap = publicConfigService.listProjectItem(mapIn);

        return new ResultModel<Map<String, Object>>(retMap);
    }

    // 添加项目
    @ResponseBody
    @RequestMapping("/addProjectItem")
    public ResultModel<Map<String, Object>> addProjectItem(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in addProjectItem----arg==" + arg);

        // 基础校验
        ToolUtils.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        Map<String, Object> mapIn = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

        // 入参校验
        Map<String, Object> projectInfo = ToolUtils.typeCast(mapIn.get("projectInfo"));
        Map<String, Object> chkVar = new HashMap<String, Object>();
        chkVar.put("configCode", "题型id");
        chkVar.put("configName", "项目名称");
        // chkVar.put("queueId", "队列id");
        chkVar.put("configType", "类型编码");
        ToolUtils.checkInputInfo("projectInfo", projectInfo, chkVar);

        chkVar.clear();
        Map<String, Object> jsonOpInfo = ToolUtils.typeCast(mapIn.get("oprInfo"));
        chkVar.put("opNote", "操作备注");
        chkVar.put("opCode", "操作编码");
        chkVar.put("opType", "操作类型");
        ToolUtils.checkInputInfo("oprInfo", jsonOpInfo, chkVar);
        jsonOpInfo.put("updateTime", DateUtil.getSysDateA());// 操作时间
        jsonOpInfo.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));// 操作流水

        // 登录信息，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        mapIn.put("sessionInfo", sessionMap);

        Map<String, Object> retMap = publicConfigService.saveNewProjectItem(mapIn);

        return new ResultModel<Map<String, Object>>(retMap);
    }

    // 修改项目
    @ResponseBody
    @RequestMapping("/updateProjectItem")
    public ResultModel<Map<String, Object>> updateProjectItem(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in updateProjectItem----arg==" + arg);

        // 基础校验
        ToolUtils.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        Map<String, Object> mapIn = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

        // 入参校验
        Map<String, Object> projectInfo = ToolUtils.typeCast(mapIn.get("projectInfo"));
        Map<String, Object> chkVar = new HashMap<String, Object>();
        chkVar.put("configCode", "题型id");
        chkVar.put("id", "标签(项目)id");
        chkVar.put("configType", "类型编码");
        ToolUtils.checkInputInfo("projectInfo", projectInfo, chkVar);
        String showFlag = (String) projectInfo.get("showFlag");
        if (StringUtils.isNotBlank(showFlag) && !Constants.CONTENT_CONFIG_SHOW_FLAG_00.equals(showFlag)
                && !Constants.CONTENT_CONFIG_SHOW_FLAG_01.equals(showFlag)) {
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
                    Constants.API_INPUT_PARAMS_DATA_ERROR + "，展示状态传值错误！"));
        }

        chkVar.clear();
        Map<String, Object> jsonOpInfo = ToolUtils.typeCast(mapIn.get("oprInfo"));
        chkVar.put("opNote", "操作备注");
        chkVar.put("opCode", "操作编码");
        chkVar.put("opType", "操作类型");
        ToolUtils.checkInputInfo("oprInfo", jsonOpInfo, chkVar);
        jsonOpInfo.put("updateTime", DateUtil.getSysDateA());// 操作时间
        jsonOpInfo.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));// 操作流水

        // 登录信息，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        mapIn.put("sessionInfo", sessionMap);

        Map<String, Object> retMap = publicConfigService.modifyProjectItem(mapIn);

        return new ResultModel<Map<String, Object>>(retMap);
    }

    // 删除项目
    @ResponseBody
    @RequestMapping("/delProjectItem")
    public ResultModel<Map<String, Object>> delProjectItem(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in delProjectItem----arg==" + arg);

        // 基础校验
        ToolUtils.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        Map<String, Object> mapIn = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

        // 入参校验
        Map<String, Object> projectInfo = ToolUtils.typeCast(mapIn.get("projectInfo"));
        Map<String, Object> chkVar = new HashMap<String, Object>();
        chkVar.put("configCode", "题型id");
        chkVar.put("id", "标签(项目)id");
        chkVar.put("configType", "类型编码");
        ToolUtils.checkInputInfo("projectInfo", projectInfo, chkVar);

        chkVar.clear();
        Map<String, Object> jsonOpInfo = ToolUtils.typeCast(mapIn.get("oprInfo"));
        chkVar.put("opNote", "操作备注");
        chkVar.put("opCode", "操作编码");
        chkVar.put("opType", "操作类型");
        ToolUtils.checkInputInfo("oprInfo", jsonOpInfo, chkVar);
        jsonOpInfo.put("updateTime", DateUtil.getSysDateA());// 操作时间
        jsonOpInfo.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));// 操作流水

        // 登录信息，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        mapIn.put("sessionInfo", sessionMap);

        // 删除就是置状态为无效
        projectInfo.put("status", Constants.TABLE_RECORD_STATUS_INVALID);
        Map<String, Object> retMap = publicConfigService.modifyProjectItem(mapIn);

        return new ResultModel<Map<String, Object>>(retMap);
    }
}
